function layoutFileViewPort(layout, node) {
    var uri = node.attributes.uri;
    var name = node.attributes.name;
    var parentId = node.parentNode.id;
    layout.setActiveItem("main-content-file-panel");

    var downloadDiv = Ext.getDom("main-content-file-download");
    downloadDiv.innerHTML = "";
    var downloadHtml = "<a href='" + uri + "' target='_blank'>Download '" + name + "'</a>";
    Ext.DomHelper.insertHtml("beforeEnd", downloadDiv, downloadHtml);

    Ext.Ajax.request({
        url: uri + "/mayUpdate",
        method: "GET",
        params: {
            "user_uri": LoggedInUser.userUri
        },
        success: function() {

            renderRenameFileForm(node);

            var renameDiv = Ext.getDom("main-content-file-rename");
            Ext.DomHelper.insertHtml("beforeEnd", renameDiv, "<br>");

            var deleteFileDiv = Ext.getDom("main-content-file-delete");
            deleteFileDiv.innerHTML = "";

            new Ext.Button({
                id: 'delete-file-button',
                text: "Delete File " + node.attributes.name + "'",
                renderTo: deleteFileDiv,
                listeners: {click:
                        function() {
                            deleteFile(name, uri, parentId);
                        }
                }
            });

            showFilePreview(node, uri);
        },
        failure: function() {
            Ext.getDom("main-content-file-delete").innerHTML = "";
            Ext.getDom("main-content-file-rename").innerHTML = "";
            showFilePreview(node, uri);
        }
    });
}

function showFilePreview(node, uri) {
    var previewDiv = Ext.getDom("main-content-file-preview");
    previewDiv.innerHTML = "";
    if (node.attributes.mimeType) {
        if (node.attributes.mimeType.substring(0, 5) == "image") {
            var html = "";
            html += "<a href='" + uri + "' target='_blank'>";
            html += "<img src='" + uri + "' width='50%' heigth='50%'/>";
            html += "</a>";
            Ext.DomHelper.insertHtml("beforeEnd", previewDiv, html);
        }
    }
}

function renderRenameFileForm(node) {
    Ext.getDom("main-content-file-rename").innerHTML = "";
    var uri = node.attributes.uri;
    var originalName = node.attributes.name;
    var parentNodeId = node.parentNode.id;
    var renameFileFormPanel = new Ext.form.FormPanel({
        id: 'fileRenameSubForm',
        title: 'Rename File',
        method: 'POST',
        applyTo: "main-content-file-rename",
        border: true,
        items: [
            new Ext.form.FieldSet({
                autoHeight: true,
                autoWidth: true,
                border: false,
                items: [
                    new Ext.form.TextField({
                        fieldLabel: 'New Name',
                        defaultAutoCreate : {tag:"input", size: "40", autocomplete: "off"},
                        name: 'renameFile',
                        id: 'renameFileId',
                        allowBlank: false
                    }),
                    new Ext.Button({
                        id: 'show-button',
                        text: 'Update',
                        listeners: {
                            click: function() {
                                var newFileName = Ext.getCmp("renameFileId").getValue();
                                var postUrl = uri + "/rename";
                                Ext.Ajax.request({
                                    url: postUrl,
                                    method: "POST",
                                    params: {
                                        "new_name": newFileName
                                    },
                                    success: function() {
                                        eventManager.fireStatusMessageEvent({ text: "File renamed to: " + newFileName, level: "info" });
                                        node.attributes.uri = uri.substring(0, uri.lastIndexOf(originalName)) + newFileName;
                                        node.attributes.name = newFileName;
                                        eventManager.fireNodeSelectEvent(node);
                                        var parentNode = tree.getNodeById(parentNodeId);
                                        expandNode(parentNode);
                                    },
                                    failure: function() {
                                        eventManager.fireStatusMessageEvent({ text: "Failed to rename", level: "error" });
                                    }
                                });
                            }
                        }
                    })
                ]
            })
        ]
    });
}

function deleteFile(name, url, parentNodeId)
{
    Ext.MessageBox.confirm('Message', 'Delete ' + name + '?', function(btn) {
        if (btn == 'yes') {
            var deleteUrl = url + "/delete";
            Ext.Ajax.request({
                url: deleteUrl,
                method: "POST",
                success: function() {
                    eventManager.fireStatusMessageEvent({ text: name + " Deleted", level: "info"});
                    var parentNode = tree.getNodeById(parentNodeId);
                    expandNode(parentNode);
                    eventManager.fireNodeSelectEvent(parentNode);
                },
                failure: function() {
                    eventManager.fireStatusMessageEvent({ text: "Deleted Failed for " + name, level: "error"});
                }
            });

        }
    });

}